import { v4 as uuid } from 'uuid'

/** 取消索引 */
function removeIndex(editor, node) {
  editor.dom.removeAllAttribs(node)
  editor.selection.select(node)
  editor.formatter.remove('bizIndex')
  editor.fire('indexRemove')
}


export default function (editor) {
  // 索引
  editor.ui.registry.addToggleMenuItem('contextmenu_bizindex', {
    text: '索引',
    onAction() {
      const content = editor.selection.getContent({ format: 'text' })
      const currentNode = editor.selection.getNode()
      const prevListTitleNode = editor.dom.getPrev(currentNode, '.list-title')
      const contentNumber = prevListTitleNode?.dataset?.blockIndex || prevListTitleNode?.dataset?.index
      editor.formatter.apply('bizIndex', { dataId: uuid(), content, contentNumber })
      editor.fire('indexAdd')
    }
  })
  // 取消索引
  editor.ui.registry.addToggleMenuItem('contextmenu_bizindex_cancle', {
    text: '取消索引',
    onAction() {
      removeIndex(editor, editor.selection.getNode())
    }
  })

  // console.debug('---右键菜单: 索引---')
}
